package demo8;

import java.util.Stack;

/**时间复杂度：O(n)-O(n2)
 *
 */
public class Sort {
    public void insertSort(int[] arr) {
        int i = 0;
        for(i = 1; i < arr.length; i++) {
            int j = i - 1;
            int tmp = arr[i];
            while (j >= 0) {
                if(arr[j] > tmp) {
                    arr[j + 1] = arr[j];
                }else {
                    arr[j + 1] = tmp;
                    break;
                }
                j--;
            }
            arr[j + 1] = tmp;
        }
    }

    private void shell(int[] arr, int gap) {
        for(int i = gap; i < arr.length; i++) {
            int j = i - gap;
            int tmp = arr[i];
            while (j >= 0) {
                if(arr[j] > tmp) {
                    swap(arr,j,j+gap);
                }else {
                    break;
                }
                j--;
            }
        }
    }
    public void shellSort(int[] arr) {
        int gap = arr.length;
        while(gap > 1) {
            gap = gap / 2;
            shell(arr,gap);
        }
    }

    public void selectSortSlowly(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            int minIndex = i;
            for (int j = i + 1; j < arr.length; j++) {
                if(arr[j] < arr[minIndex]) {
                    minIndex = j;
                }
            }
            swap(arr,i,minIndex);
        }
    }

    private void swap(int[] arr, int i, int j) {
        int tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
    }

    public void selectSortFastly(int[] arr) {
        int right = arr.length - 1;
        int left = 0;
        int minIndex = 0;
        int maxIndex = 0;
        while(left < right) {
            minIndex = left;
            maxIndex = left;
            for (int i = left + 1; i <= right; i++) {
                if(arr[i] < arr[minIndex]) {
                    minIndex = i;
                }
                if(arr[i] > arr[maxIndex]) {
                    maxIndex = i;
                }
            }
            //有一种情况需要注意，就是最大值就是第一个元素，eg：91,2,5,3,6,4,7,9，按照下面方法会出错
            //swap(arr,left,minIndex);
            //swap(arr,right,maxIndex);
            //但可以修改为
//            swap(arr,left,minIndex);
//            if(maxIndex == left) {
//                maxIndex = minIndex;
//            }
//            swap(arr,right,maxIndex);
            swap(arr,right,maxIndex);
            swap(arr,left,minIndex);
            left++;
            right--;
        }
    }

    public void bubbleSort(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr.length - 1 - i; j++) {
                //从小到大排
                if(arr[j] > arr[j + 1]) {
                    swap(arr,j,j+1);
                }
            }
        }
    }

    //优化
    public void bubbleSort1(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            boolean flag = false;
            for (int j = 0; j < arr.length - 1 - i; j++) {
                //从小到大排
                if(arr[j] > arr[j + 1]) {
                    swap(arr,j,j+1);
                    flag = true;
                }
            }
            if(!flag) {
                //代表此时已经有序
                break;
            }
        }
    }

    //堆排序
    private void softDown(int[] arr,int parent,int size) {
        int child = parent * 2 + 1;
        while(child < size) {
            if(child + 1 < size && arr[child] < arr[child + 1]) {
                child++;
            }
            swap(arr,child,parent);
            parent = child;
            child = parent * 2 + 1;
        }
    }
    //创建大根堆
    private void createMaxHeap(int[] arr) {
        for(int parent = (arr.length - 1 - 1) / 2; parent >= 0; parent--) {
            softDown(arr,parent,arr.length);
        }
    }
    public void heapSort(int[] arr) {
        createMaxHeap(arr);
        int size = arr.length;
        while(size > 0) {
            swap(arr,0,size - 1);
            softDown(arr,0,size - 1);
            size--;
        }
    }

    //快速排序:91,2,5,3,6,4,7,9

    //1.挖坑法
    private int partition(int[] arr,int left, int right) {
        int tmp = arr[left];
        while(left < right) {
            while(left < right && arr[right] >= tmp) {
                right--;
            }
            arr[left] = arr[right];
            while(left < right && arr[left] <= tmp) {
                left++;
            }
            arr[right] = arr[left];
        }
        arr[left] = tmp;
        return left;
    }
    //2.Hoare法
    private int partition1(int[] arr,int left, int right) {
        int tmp = arr[left];
        int tmpLeft = left;
        while(left < right) {
            while(left < right && arr[right] >= tmp) {
                right--;
            }
            while(left < right && arr[left] <= tmp) {
                left++;
            }
            swap(arr,left,right);
        }
        swap(arr,tmpLeft,right);
        return left;
    }
    private void quick(int[] arr,int left, int right) {
        if(left >= right) {
            return;
        }
        //优化2：
        if(right - left + 1 < 10) {
            //直接用插入排序
            insertSort(arr,left,right);
        }
        //优化1:三数取中法
        int mid = findMid(arr,left,right);
        swap(arr,mid,left);
        int midIndex = partition(arr,left,right);
        quick(arr,left,midIndex - 1);
        quick(arr,midIndex + 1,right);

    }

    private void insertSort(int[] arr, int left, int right) {
        for(int i = left + 1; i <= right; i++) {
            int j = i - 1;
            int tmp = arr[i];
            while(j >= 0) {
                if(arr[j] > tmp) {
                    swap(arr,j+1,j);
                }else {
                    break;
                }
                j--;
            }

        }
    }

    private int findMid(int[] arr, int left,int right) {
        int midIndex = (right - left)/2;
        if(arr[left] > arr[right]) {
            if(arr[midIndex] < arr[right]) {
                return right;
            }else if(arr[midIndex] > arr[left]) {
                return left;
            }else {
                return midIndex;
            }
        }else {
            if(arr[midIndex] < arr[left]) {
                return left;
            }else if(arr[midIndex] > arr[right]) {
                return right;
            }else {
                return midIndex;
            }
        }
    }

    public void quickSort(int[] arr) {
        quick(arr,0,arr.length - 1);
    }

    //快排非递归
    public void quickSortNor(int[] arr) {
        Stack<Integer> stack = new Stack<>();
        int left = 0;
        int right = arr.length-1;
        int midIndex = partition(arr,left,right);
        stack.push(midIndex+1);
        stack.push(right);
        stack.push(left);
        stack.push(midIndex-1);
        while(!stack.empty()) {
            right = stack.pop();
            left = stack.pop();
            if(left >= right) {
                continue;
            }
            midIndex = partition(arr,left,right);
            stack.push(midIndex+1);
            stack.push(right);
            stack.push(left);
            stack.push(midIndex-1);
        }
    }
    //归并排序
    public void mergeSort(int[] arr) {
        mergesort(arr,0, arr.length-1);
    }

    private void mergesort(int[] arr, int left, int right) {
        if(left >= right) {
            return;
        }
        int mid = (left + right - 1) / 2;

        mergesort(arr,left,mid);
        mergesort(arr,mid+1,right);

        //合并
        merge(arr,left,mid,right);

    }

    private void merge(int[] arr, int left, int mid, int right) {
        int s1 = left;
        int e1 = mid;
        int s2 = mid + 1;
        int e2 = right;
        int size = right - left + 1;
        int[] ret = new int[size];
        int k = 0;
        while(s1 <= e1 && s2 <= e2) {
            if(arr[s1] < arr[s2]) {
                ret[k++] = arr[s1++];
            }else {
                ret[k++] = arr[s2++];
            }
        }
        while(s1 <= e1) {
            ret[k++] = arr[s1++];
        }
        while(s2 <= e2) {
            ret[k++] = arr[s2++];
        }
        for(int i = 0; i < ret.length; i++) {
            arr[i+left] = ret[i];
        }
    }


    //非递归实现
    public void mergeSortNor(int[] arr) {
        int gap = 1;
        while(gap < arr.length) {
            for(int i = 0; i < arr.length; i = i + gap * 2) {
                int left = i;
                int mid = left + gap - 1;
                int right = mid + gap;
                if(mid >= arr.length) {
                    mid = arr.length - 1;
                }
                if(right >= arr.length) {
                    right = arr.length - 1;
                }
                merge(arr,left,mid,right);
            }
            gap *= 2;
        }
    }

    //计数排序
    public void countSort(int[] arr) {
        int max = arr[0];
        int min = arr[0];
        for(int i = 0; i < arr.length; i++) {
            if(arr[i] < min) {
                min = arr[i];
            }
            if(arr[i] > max) {
                max = arr[i];
            }
        }
        int size = max - min + 1;
        int[] count = new int[size];
        for(int j = 0; j < arr.length; j++) {
            count[arr[j] - min]++;
        }
        int k = 0;
        for(int i = 0; i < size; i++) {
            while(count[i] != 0) {
                arr[k++] = i + min;
                count[i]--;
            }
        }
    }
}
